﻿using Biomedica.Graphics;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NuiDicomPlayer.DSP
{
    public class DSP
    {
        private Queue<double> Buffer;
        int Limit { set; get; }
        public Double[] buffer { set; get; }
        public DSP(int size)
        {
            Buffer = new Queue<double>();
            Limit = size;
            

        }
        public void Add(double v)
        {
            Buffer.Enqueue(v);
            if (Buffer.Count > Limit) Buffer.Dequeue();
        }
        public Double MA(Double data)
        {
            Add(data);
            double ma=0;
            for(int i=0;i<buffer.Length;i++) ma+=buffer[0];          
            return ma/buffer.Length;
        }
        public double ExponentialWeightedAvg(Double data)
        {
            Add(data);
            double y = ExponentialMovingAverage(Buffer.ToArray(), data);
            return y;
           

        }
        public double ExponentialMovingAverage(double[] data, double baseValue)
        {
            double numerator = 0;
            double denominator = 0;

            double average = data.Sum();
            average /= data.Length;

            for (int i = 0; i < data.Length; ++i)
            {
                numerator += data[i] * Math.Pow(baseValue, data.Length - i - 1);
                denominator += Math.Pow(baseValue, data.Length - i - 1);
            }

            numerator += average * Math.Pow(baseValue, data.Length);
            denominator += Math.Pow(baseValue, data.Length);

            return numerator / denominator;
        }
        public double WeightedAverage(double[] data, double[] weights)
        {
            if (data.Length != weights.Length)
            {
                return Double.MinValue;
            }

            double weightedAverage = data.Select((t, i) => t * weights[i]).Sum();

            return weightedAverage / weights.Sum();
        }
        public static double FindDeltaAvg(Vector3[] SkeletonBuffer)
        {
            Vector3 maV = Vector3.Zero;
           
            Vector3 old_s=null;
            Vector3 delta = Vector3.Zero;
            foreach (Vector3 s in SkeletonBuffer)
            {

                /// Calculate the MA of center of gravity using Vecdtor3
                /// Ignore any error
                
                if(old_s!=null)
                {

                    delta = delta + (new Vector3(old_s.X, old_s.Y, old_s.Z) - new Vector3(s.X, s.Y, s.Z));
                }
                old_s=s;

            }
            double max = delta.X / SkeletonBuffer.Length;
            double may = delta.X / SkeletonBuffer.Length;
            double maz = delta.X / SkeletonBuffer.Length;

            return System.Math.Sqrt((max * max) + (may * may) + (maz * maz));
        }
         public static Vector3 FindMA(Vector3[] SkeletonBuffer)
        {
            Vector3 maV = Vector3.One;
            double ma = 0;
            foreach (Vector3 s in SkeletonBuffer)
            {

                /// Calculate the MA of center of gravity using Vecdtor3
                /// Ignore any error
                if(s!=null)
                maV += s;


            }

            return maV / SkeletonBuffer.Length;
        }
    

    }
}
